package com.colorlight.cloud.complete.entity.event;

import com.colorlight.cloud.complete.api.CompleteEntityEvent;
import com.colorlight.cloud.complete.entity.enumerate.UpdateResult;
import com.colorlight.iot.proto.complete.event.CompleteEvent;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 程序更新上报事件
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class UpdateReport implements CompleteEntityEvent {

    /**
     * 更新结果
     */
    private UpdateResult result;

    @Override
    public UpdateReport byteConvertObject(ByteString bytes) {
        CompleteEvent.UpdateReport updateReport = null;
        try {
            updateReport = CompleteEvent.UpdateReport.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        Objects.requireNonNull(updateReport);
        this.setResult(UpdateResult.getUpdateResult(updateReport.getResult()));
        return this;
    }
}

